<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: PHP@妖孽 <dino_ma@163.com>
// +----------------------------------------------------------------------------
namespace Home\Controller;
use Think\Controller;

class ApiController extends Controller {
    
    public function index(){ exit('error'); }
    
    public function _empty(){ exit('empty'); }

    /**
     * @todo    订单状态修改（回调）
     * @param   http://host/iweb/api.php?m=Index&a=orderCallBack
     */
    public function orderCallBack(){
         
        \Think\Log::write("支付接口回调：orderCallBack --- ".json_encode($_POST).PHP_EOL);
    
        $money = I('post.money');
        $param = I('post.param');
        //主参数判断
        if(empty($param) || $money <= 0){
            $this->ajaxReturn(array('status' => 0, 'info' => 'error'));
        }
        //method 支付方式
        //server_order_id 支付平台的订单号
        //client_order_no 我的订单号
        //流水
        //订单系统：3:支付宝;4:财付通;5:支付宝wap端;6:微信PC扫码;7:微信JSSDK支付;8:绑定消费
        /*
        alipay=支付宝3
        tenpay=财付通4
        wechatpay=微信扫码支付6
        wechatwappay=微信客户端支付 7
        */
        $pay_type = 0;
        switch ($param['method']){
            case 'alipay'://alipay=支付宝3
                $pay_type = 3;
                break;
            case 'tenpay'://tenpay=财付通4
                $pay_type = 4;
                break;
            case 'wechatpay'://wechatpay=微信扫码支付6
                $pay_type = 6;
                break;
            case 'wechatwappay'://wechatwappay=微信客户端支付 7
                $pay_type = 7;
                break;
            default:
        }
        $order_amount = $money;
        $order_id = (int)$param['client_order_no'];
        //业务参数错误
        if($pay_type == 0 || $order_id < 1){
            $this->ajaxReturn(array('status' => 0, 'info' => 'params error'));
        }
        //获取订单信息
        $field = 'order_id,check_code';
        $where = array(
            'order_id' => $order_id,
            'order_type' => 2,
            'order_status' => array('lt', 2),
        );
        $order = M('order')->field($field)->where($where)->find();
        //订单不存在
        if(!$order){
            $this->ajaxReturn(array('status' => 0, 'info' => 'order is null'));
        }
    
        //订单修改
        $check_code = $order['check_code'];
        M('order')->startTrans();
        $data['order_status'] = 2;
        $data['order_amount'] = $order_amount;
        $data['order_pay_amount'] = $order_amount;
        $data['server_order_id'] = $param['server_order_id'];//支付网关订单ID
        $order = M('order')->where($where)->save($data);
        //流水记录
        $payment = D('Admin/Order')->orderPaymentAdd($order_id, $order_amount, $pay_type, 1);
        if($order !== false && $payment !== false){
            M('order')->commit();
            //商品订单表状态修改
            M('shop_order')->where(array('order_id' => $order_id))->setField('status', 1);
            //订单日志
            D('Admin/Order')->orderLogAdd($order_id, $order_amount, $pay_type, 2);
            //回调函数
            //new send sms
            D('Api/Order', 'Service')->sendOrderSms($param['client_order_no'], $check_code);
            //old
            //$this->toCurl(array('money' => $money, 'param' => $param));
            //返回结果
            $this->ajaxReturn(array('status' => 1, 'info' => 'success'));
        }else{
            M('order')->rollback();
            $this->ajaxReturn(array('status' => 0, 'info' => 'update error'));
        }
    }
    
}
